package net.thxjj.service.impl;

import net.model.TypeCmd;
import net.model.device.ProductBean;
import net.model.device.PropertyBean;
import net.thxjj.error.ErrCode;
import net.thxjj.service.IProductService;
import net.thxjj.service.dao.ProductDao;
import net.util.ParameterMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

@Service
public class ProductService implements IProductService {
    @Autowired ProductDao productDao;

    @Override
    public ProductBean getProductBean(String prodKey) {
        return productDao.getProdBean(prodKey);
    }

    @Override
    public ParameterMap getProductPropertys(long cmd, String prodKey, TypeCmd typeCmd) {
        ParameterMap parameterMap = new ParameterMap();
        parameterMap.put("cmd_id",cmd);
        parameterMap.put("prod_key",prodKey);
        parameterMap.put("type",typeCmd.getType());
        ParameterMap result = productDao.getCmdBean(parameterMap);
        return result;
    }

    @Override
    public ErrCode isLawFul(String content, Map<String, Object> info) {
        String[] de = content.split(",");
        ErrCode flag = ErrCode.SUCCESS;

        for(Map.Entry<String, Object> entry : info.entrySet()){
            String mapKey = entry.getKey();
            Object mapValue = entry.getValue();
            if(!"cmdId".equals(mapKey)){
                boolean flag_has = false;
                for (String des:de){
                    long perty_id = Integer.parseInt(des);
                    PropertyBean propertyBean = productDao.getProperBean(perty_id);
                    if(propertyBean!=null){

                        if(mapKey.equals(propertyBean.getIdentifier())){
                            flag_has = true;
                            switch ((int) propertyBean.getType()){
                                case 1:
                                    try {
                                        int reslut = (int)mapValue;
                                        if(reslut>propertyBean.getMax()){
                                            flag = ErrCode.BEYOND_UPPER_LIMIT_ERROR;
                                        }else if(reslut<propertyBean.getMin()){
                                            flag = ErrCode.BEYOND_LOWER_LIMIT_ERROR;
                                        }
                                    }catch (ClassCastException e){
                                        flag = ErrCode.FIELD_INVALID_ERROR;
                                    }

                                    break;
                                case 2:
                                    try {
                                        String val = (String)info.get(propertyBean.getIdentifier());
                                        if(StringUtils.isEmpty(val)){
                                            flag = ErrCode.FIELD_INVALID_ERROR;
                                        }
                                    }catch (Exception e){
                                        flag = ErrCode.FIELD_INVALID_ERROR;
                                    }

                                    break;
                                case 3:
                                    try {
                                        BigDecimal reslut2 = (BigDecimal)info.get(propertyBean.getIdentifier());
                                        if(reslut2.floatValue()>propertyBean.getMax()){
                                            flag = ErrCode.BEYOND_UPPER_LIMIT_ERROR;
                                        }else if(reslut2.floatValue()<propertyBean.getMin()){
                                            flag = ErrCode.BEYOND_LOWER_LIMIT_ERROR;
                                        }
                                    }catch (ClassCastException e){
                                        flag = ErrCode.FIELD_INVALID_ERROR;
                                    }

                                    break;
                            }
                            break;
                        }

                    }

                }

                if(flag_has==false){
                    flag = ErrCode.INSTRUCTION_NOT_EXIST_ERROR;
                    break;
                }
            }

        }


        return flag;
    }

}
